package com.bbx.taxi.client.DB;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.bbx.api.gclient.msg.CouponMsg;
import com.bbx.api.sdk.model.passanger.Return.Coupon.CouponList;
import com.bbx.api.sdk.model.passanger.Return.Coupon.Coupons;
import com.bbx.api.sdk.model.passanger.Return.Coupon.Metadata;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.DB.Util.DBHelper;
import com.bbx.taxi.client.DB.Util.DBcolumns;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;

/**
 *优惠劵
 *
 */
public class CouponDB  implements DBcolumns{
	private DBHelper helper;
	public CouponDB(Context context) {
		MyApplication myApplication=MyApplication.getInstance();
		helper = DBHelper.getInstance(context,myApplication.getUid());
	}
	/**
	 *  打开只读的数据库
	 */
	public SQLiteDatabase onOpenRead() { 
		SQLiteDatabase SQLdb = helper.getReadableDatabase();
		return SQLdb;
	}

	/**
	 *  打开只写的数据库
	 */
	public SQLiteDatabase onOpenWrite() { 
		SQLiteDatabase SQLdb = helper.getWritableDatabase();
		return SQLdb;
	}
	/**
	 *查询全部
	 */
	public Cursor onQuery() {
		SQLiteDatabase SQLdb=onOpenRead();
		Cursor cursor = SQLdb.query(TABLE_COUPON, null, null, null, null,
				null, COUPON_id + " ");
		return cursor;

	}
	/**
	 *用于分页查询，指定数量，指定id,按降序查询，不能大于id
	 */
	public Cursor onQuery(int id,int count) {
		SQLiteDatabase SQLdb=onOpenRead();
		Cursor cursor = null;
		if(id==-1){
			 cursor = SQLdb.query(TABLE_COUPON, null,null, null, null,
						null, COUPON_id + "  LIMIT 0,"+count);
		}
		else{
			 cursor = SQLdb.query(TABLE_COUPON, null,COUPON_id+"<"+id, null, null,
					null, COUPON_id + "  LIMIT "+count);
		}
		
		return cursor;

	}
	/**
	 * 删除整个表
	 */
	public void onDelete() {
		SQLiteDatabase SQLdb=onOpenWrite();
		SQLdb.delete(TABLE_COUPON, null, null);
		SQLdb.close();
	}

	/**
	 * 删除
	 */
	public void onDelete(String rowId) {
		SQLiteDatabase SQLdb=onOpenWrite();
		 String[] whereArgs = { String.valueOf(rowId) };
		 int i =SQLdb.delete(TABLE_COUPON, COUPON_id + "=?", whereArgs);
		 SQLdb.close();
	}
	/**
	 *更新一条数据
	 */
	public void onUpdate(String id,String coupon_id,String sum, String  expire_time,String usabletime,String usabletype,String usableorder,String usableline) {
		SQLiteDatabase SQLdb=onOpenWrite();
		ContentValues cv = new ContentValues();
		cv.put(COUPON_ID, coupon_id);
		cv.put(COUPON_SUM, sum);
		cv.put(COUPON_EXPIRE_TIME, expire_time);
		cv.put(COUPON_EXPIRE_TIME, expire_time);
		cv.put(COUPON_USABLETIME, usabletime);
		cv.put(COUPON_USABLETYPE, usabletype);
		cv.put(COUPON_USABLEORDER, usableorder);
		cv.put(COUPON_USABLELINE, usableline);
		SQLdb.update(TABLE_COUPON, cv, COUPON_id + "=" + id, null);
		SQLdb.close();
	}
	/**
	 * 插入一条数据 
	 */
	public void onWrite(int coupon_id,String sum, String  expire_time,String usabletime,String usabletype,String usableorder,String usableline) {
		SQLiteDatabase SQLdb=onOpenWrite();
		ContentValues values = new ContentValues();
		values.put(COUPON_ID, coupon_id);
		values.put(COUPON_SUM, sum);
		values.put(COUPON_EXPIRE_TIME, expire_time);
		values.put(COUPON_USABLETIME, usabletime);
		values.put(COUPON_USABLETYPE, usabletype);
		values.put(COUPON_USABLEORDER, usableorder);
		values.put(COUPON_USABLELINE, usableline);
		SQLdb.insert(TABLE_COUPON, COUPON_id, values);
		SQLdb.close();
	}
	/**
	 * 写入数据库数据,
	 */
	public void onWriteDB(final List<Coupons> list) {
		new AsyncTask<Void, Void, Void>(){

			@Override
			protected Void doInBackground(Void... params) {
				for (int i = 0; i < list.size(); i++) {
					int coupon_id = list.get(i).getId();
					String sum = list.get(i).getSum()+"";
					String expire_time = list.get(i).getExpire_time();
					String usabletime ="";
					String usabletype = "";
					String usableorder = "";
					String usableline = "";
		           try{
		        	   usabletime= list.get(i).getMetadata().getUsabletime();
		        	   usabletype= list.get(i).getMetadata().getUsabletype();
		        	   usableorder=list.get(i).getMetadata().getUsableorder();
		        	   usableline = list.get(i).getMetadata().getUsableline().toString();
		           }
		           catch(Exception e){}
					onWrite(coupon_id, sum, expire_time,
							usabletime, usabletype, usableorder, usableline);
				}
				return null;
			}}.execute();

	}
	/**
	 * 刚进来的时候读取的
	 */
	public List<Coupons> onReadDB(int type,int num) {
		List<Coupons> list=new ArrayList<Coupons>();
		Cursor c = onQuery(type, num);
		c.moveToFirst();
		if (c.getCount() > 0) {
			do {

				Map<String, String> map = new HashMap<String, String>();
				for (int i = 0; i < c.getColumnCount(); i++) {
					map.put(c.getColumnName(i), c.getString(i));
				}
				Coupons coupons = new Coupons();
				coupons.setId(Integer.parseInt(map.get(DBcolumns.COUPON_ID)));
				coupons.setSum(Integer.parseInt(map.get(DBcolumns.COUPON_SUM)));
				coupons.setExpire_time(map.get(DBcolumns.COUPON_EXPIRE_TIME));
				Metadata mMetadata=new Metadata();
				mMetadata.setUsabletime(map.get(DBcolumns.COUPON_USABLETIME));
				mMetadata.setUsabletype(map.get(DBcolumns.COUPON_USABLETYPE));
				mMetadata.setUsableorder(map.get(DBcolumns.COUPON_USABLEORDER));
				
				JSONArray ja_line;
				try {
					ja_line = new JSONArray(map.get(DBcolumns.COUPON_USABLELINE));
				List<String> list_line=new ArrayList<String>();
				for(int j=0;j<ja_line.length();j++){
					list_line.add(ja_line.get(j).toString());
				}
				mMetadata.setUsableline(list_line);
				} catch (JSONException e) {
					e.printStackTrace();
				}
				coupons.setMetadata(mMetadata);
				list.add(coupons);
			} while (c.moveToNext());
		}
		c.close();
		onColse();
		return list;
	}
	/**
	 * 关闭数据库
	 */
	public void onColse(){
		SQLiteDatabase SQLdb=onOpenWrite();
		SQLdb.close();
	}
}
